package com.hainatong.shiphelper.module.OAmgr.fragment;

import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.entity.MsgBean;
import com.hainatong.shiphelper.module.OAmgr.viewmodel.OAShenPiViewModel;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.module.OAmgr.adapter.ShenPiMsgAdapter;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.hainatong.mvvmbase.rxbus.RxBus;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import io.reactivex.disposables.Disposable;


public class ExamineFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, OAShenPiViewModel>
{

    private ShenPiMsgAdapter adapter;

    public static ExamineFragment newInstance()
    {
        ExamineFragment fragment = new ExamineFragment();
        return fragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }


    @Override
    protected void initView(View view)
    {
        adapter = new ShenPiMsgAdapter();

        RecyclerViewHelper.addLinerLayoutMgr( viewDataBinding.recyclerview , new ListItemDecoration(SizeUtils.dp2px(10)));
        viewDataBinding.recyclerview.setAdapter( adapter );

        viewDataBinding.refreshlayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout1 -> {
            viewModel.tryRefreshShenPiMs();
        } );
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout2 -> {
            viewModel.loadMoreShenPiMs();
        } );

        //审核消息 点击
        adapter.setOnItemClickListener((adapter1, view1, position) -> {

            MsgBean msgBean = adapter.getItem(position);
            //1休假审批,2报废申请,3值班交接,4离职交接,5上下船交接

            if( msgBean.getOa_type().equals("1") )
            {
                ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_OA_SHENPI_APPLY, GsonUtil.obj2json(msgBean));
            }
            else if( msgBean.getOa_type().equals("2") )
            {
                ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_OA_BROKEN_APPLY, GsonUtil.obj2json(msgBean));
            }
            else
            {

            }

        } );
    }

    private void initView()
    {
        // 加载 gif 动画
        /*
        Glide.with(getActivity())
             .load(R.drawable.gif_test1)
             .transition(DrawableTransitionOptions.withCrossFade(300))
             .into(viewDataBinding.ivGif);
        */
        initRxBus();
    }

    private void initRxBus()
    {
        Disposable disposable = RxBus.getDefault().toObservable(1001 , RxBusBaseMessage.class)
                          .subscribe( msg -> {
                                  if( msg.getCode() == 100  )
                                  {

                                  }
                              });
        addSubscription( disposable );
    }

    @Override
    protected void lazyLoad()
    {
        String useId = GlobalConfig.getInstance().getUserInfo().getUuid();
        //查询 审批消息
        viewModel.queryShenPiMsgByUserID( useId );
        viewModel.getShenPiMsgResult().observe( this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        //处理 结果
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {

                        adapter.addData( result.getData() );
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        } );
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

}
